/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.webhomesites.padrao.utils;

import java.awt.event.KeyEvent;

/**
 *
 * @author Webhome
 */
public class Mascarador {
   
    public final String DATA = "DATA";
    public final String RG = "RG";
    public final String CPF = "CPF";
    public final String CNPJ = "CNPJ";
    public final String TELEFONE = "TELEFONE";
    private String formato;
    
    public String getMascara(String campo, String tipo, KeyEvent e) {
        
        switch (tipo) {
        
            case "CNPJ": {
                return getCnpj(campo, e);
            }
            case "CPF": {
                return getCpf(campo, e);
            }
            case "TELEFONE": {
                return getTelefone(campo,e);
            }
            case "RG": {
                return getRg(campo, e);
            }
            case "DATA": {
                return getData(campo,e);
            }                
            default: 
                return null;
            }
        } 
    
    private String getData(String campo, KeyEvent e) {
       
        if(campo.length() == 2 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE) {
             
            return campo.replace(campo, campo + "/");
            } else if(campo.length() == 5 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + "/");
            } else   
            return campo;        
    }
    
    private String getTelefone(String campo, KeyEvent e) {
       
        if(campo.length() == 1 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE) {
            return campo.replace(campo, "(" + campo);
        } else if(campo.length() == 3 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + ")");
        } else if(campo.length() == 8 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + "-");
        } else   
            return campo;        
    }
    
    private String getCnpj(String campo, KeyEvent e) {
       
        if(campo.length() == 2 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE) {
            return campo.replace(campo, campo + ".");
        } else if(campo.length() == 6 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + ".");
        } else if(campo.length() == 10 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + "/");
        } else if(campo.length() == 15 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + "-");
        } else   
            return campo;        
    }
    
    private String getCpf(String campo, KeyEvent e) {
       
        if(campo.length() == 3 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE) {
            return campo.replace(campo, campo + ".");
        } else if(campo.length() == 7 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + ".");
        } else if(campo.length() == 11 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE){
            return campo.replace(campo, campo + "-");
        } else   
            return campo;        
    }
    
    private String getRg(String campo, KeyEvent e) {
       
        if(campo.length() == 8 && e.getKeyCode() != KeyEvent.VK_BACK_SPACE && e.getKeyCode() != KeyEvent.VK_DELETE) {
            return campo.replace(campo, campo + "-");
        } else   
            return campo;        
    }
    
    
    private String getFormato() {
        
        return formato;
        
    }

}
    

